package com.zhende.hr.config;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.zhende.hr.utils.R;

import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    // 处理 JSON 解析错误
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<Object> handleJsonParseException(HttpMessageNotReadableException ex) {
        // 你可以根据需要自定义错误信息
        String errorMessage = "json 结构异常: " + ex.getMostSpecificCause().getMessage();
        R r = R.error("errorMessage");
        // 返回自定义的错误响应
        return ResponseEntity.status(HttpStatus.OK).body(r);
    }

    @ExceptionHandler(AccessDeniedException.class)
    public ResponseEntity<R> handleAccessDeniedException(AccessDeniedException ex) {
        R errorResponse = R.error("权限不足");
        return new ResponseEntity<>(errorResponse, HttpStatus.OK);
    }

    
    
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)  // 处理 404 错误
    public ResponseEntity<R> handleException(HttpServletRequest request, Exception ex) {
        // 自定义的错误响应
    	 R errorResponse = R.error("权限不足");
        return new ResponseEntity<>(errorResponse, HttpStatus.OK);
    }
}
